package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.TreeUtil;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity .SysMenu;
import cn.wgx.modules.sys.entity.SysMenuPermission;
import cn.wgx.modules.sys.entity.SysRolePermission;
import cn.wgx.modules.sys.entity.form.FormMenuPer;
import cn.wgx.modules.sys.services.SysMenuPermissionService;
import cn.wgx.modules.sys .services.SysMenuService;
import cn.wgx.modules.sys.services.SysRolePermissionService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "3.3 高校专用菜单权限")
@RestController
@RequestMapping(path = "api/sysMenu")
public class SysMenuController extends BaseController{

    @Resource
    SysMenuService sysMenuService;


    @Resource
    SysMenuPermissionService sysMenuPermissionService;

    @ApiOperation(value = "列表", notes = "" )
    @ApiPermission
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String, Object> getList(
            @ModelAttribute SysMenu record, 
			@RequestParam(value = "page", required = false, defaultValue = "1")Integer page,
			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,
            @ApiParam(value = "树")@RequestParam(defaultValue = "false", required = false)Boolean tree,
            @ApiParam(value = "是否倒序,默认是")@RequestParam(defaultValue = "true", required = false)Boolean desc
        ) {
        PageHelper.startPage(page, Math.min(Math.abs(pageSize), maxPageSize));
        List<SysMenu> list = sysMenuService.getList(record, desc);
        if(tree){
            return returnData(TreeUtil.getTreeArray(list, "menu_id", "pid"));
        }
        return returnData(list);
    }


    @ApiOperation(value = "获取单条菜单数据", notes = "" )
    @ApiPermission
	@RequestMapping(path = "get", method = RequestMethod.GET)
    public Map<String, Object> getOne(@RequestParam Integer menu_id) {
		return returnData(sysMenuService.getOne(menu_id));
    }

    @ApiOperation(value = "获取菜单全部已选接口", notes = "" )
    @ApiPermission
    @RequestMapping(path = "getPerByMenuId", method = RequestMethod.GET)
    public Map<String, Object> getPer(@RequestParam Integer menu_id) {
        return returnData(sysMenuService.getPerByMenuId(menu_id));
    }

    @ApiOperation(value = "新建菜单", notes = "必须参数: title, pid: 为0则为顶级菜单." )
    @ApiPermission
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertOne(@RequestBody SysMenu record) {
        if(record == null || StringUtils.isBlank(record.getTitle()) || record.getTitle().length() > 20){
            return returnErrorMap("参数错误");
        }
        if(Validation.isNotNullOrZero(record.getPid())){
            SysMenu parent = sysMenuService.getOne(record.getPid());
            if(null == parent){
                return returnErrorMap("PID错误");
            }else{
                if(parent.getPid().equals(0)){
                    record.setTopPid(parent.getMenu_id());
                }else{
                    record.setTopPid(parent.getTopPid());
                }
            }
        }
        return returnData(sysMenuService.insertOne(record));
    }

    @ApiOperation(value = "菜单授权", notes = " -> 添加/删除权限,一对多. 数据示例: {menu_id: 1, permission_ids: [1,2,3,4,5]}")
    @ApiPermission
    @RequestMapping(path = "updatePermission", method = RequestMethod.PATCH)
    public Map<String,Object> updateRolePermission(@RequestBody FormMenuPer formMenuPer) {
        if(formMenuPer == null || !Validation.isNotNullOrZero(formMenuPer.getMenu_id())){
            return returnErrorMap("参数错误");
        }else {
//            if(sysRolePermissions.get(0).getRoleId() == 1){
//                return returnErrorMap("管理员角色不能修改.");
//            }
        }
        //todo 动态更新权限
        Integer n = sysMenuPermissionService.updateMenuPermission(formMenuPer);
        return returnData(n);
    }
	
    @ApiOperation(value = "修改菜单", notes = "必须参数: menu_id. 不可修改字段: menu_id, pid" )
    @ApiPermission
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    public Map<String, Object> updateOne(@RequestBody SysMenu record) {
        return returnData(sysMenuService.updateOne(record));
    }


    @ApiOperation(value = "删除菜单", notes = "" )
    @ApiPermission
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> deleteOne(@PathVariable("id") Integer id) {
        int o = sysMenuService.deleteOne(id);
        if(o > 0){
            sysMenuPermissionService.deleteByMenuId(id);
        }
        return returnData(o);
    }

	

}